close all; clear; clc;

%tasks_num=24; agents_num=3;
load data1.mat
% taskX=randi(500,24,2);
% taskV=rand(24,2);
%agentX=randi(500,3,2);
%save('data1.mat','taskX','taskV','agentX');
agentX=[agentX;[348,124]];
tasks_num=length(taskX); agents_num=length(agentX);

for i=1:tasks_num
    %编号，类型，位置，漂移速度，时间满意度参数（两参数）
    tasks(i) = Task(i, 1, taskX(i,:)', taskV(i,:)', 10, 0.01);
end

NuLt = 0;

% tasks(1).x = [11 11]';
% tasks(2).id = 20;


%编号，最大容量，任务类型，最大航行速度，初始位置
for i=1:agents_num
    agents(i) = Agent(i, 8, [1 2], 6, agentX(i,:)',6);
end
A = zeros(length(agents));

for i=1:length(agents)
    agents(i).add_task(tasks);
    NuLt = NuLt + agents(i).Lt;
    
    % Adjacency Matrix Generation
    for k=(i+1):length(agents)
        agents(i).s(k) = 0;
        agents(k).s(i) = 0;
        if (norm(agents(k).x - agents(i).x) <= 1500)
            A(i,k) = 1;
            A(k,i) = 1;
        else
            A(i,k) = 0;
            A(k,i) = 0;
        end
    end
end

tic;
dt = 1;
Tb = 50;

for t=0:dt:Tb

    %fprintf('T = %.1f \t=======================================>>\n', t);
    for i=1:length(agents)
        nmin = min(length(tasks),NuLt);
        
        agents(i).bundle(0, nmin, 10);
        for k=1:length(agents)
            if (A(i,k) == 1)
                agents(i).auction(agents(k).id, agents(k).y, agents(k).z, agents(k).s, t);
            end
        end
                
        %fprintf('Agent-%d:', i);
        for j=1:length(agents(i).p)
            %fprintf('\t%d', agents(i).p(j));
        end
        %fprintf('\n');
    end
    
    %fprintf('\n');
    %pause(dt);
end
toc;
for i=1:length(agents)
    agents(i).sp = 0;tau=0;
    pjn = [];
    for m = 1:length(agents(i).p)
        pjn = [pjn agents(i).p(m)];
        %Task.find_by_id(this.tasks, this.p(m))
        task_m = Task.find_by_id(agents(i).tasks, agents(i).p(m));
        agents(i).sp = agents(i).sp + task_m.score(tau + agents(i).tj(pjn));
    end
    %[val(i),val_path(i)] = agents(i).t_p(this, pn); 
end

PlotAssignments(agents,tasks)
